﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Data;
using Timesheet.Service;
using Timesheet.Domain;
using DevExpress.Web;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using DevExpress.Web.Rendering;
using System.Data;

namespace Timesheet.WebForm.TimeSheets
{
    public partial class Default : System.Web.UI.Page
    {
        public ITimeSheetsService _Service { get; set; }
        public IProjectService _PrjService { get; set; }
        public ITeamService _TeamService { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            btnNew.Visible = false;
            if (!IsPostBack)
            {
                var user = (User)Session["User"];
                if (user == null)
                    Response.Redirect("~/Account/Login.aspx");
                if (user.TeamLead || user.Coordinator)
                {
                    btnNew.Visible = true;
                    var col = grvTimeSheets.Columns["colAction"] as GridViewCommandColumn;
                    col.Visible = true;
                    grvTimeSheets.Width = new Unit(grvTimeSheets.Width.Value - 130);
                }
                cbxPrj.Value = Request.Params["prjID"];
            }
        }

        protected void ds_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
        {
            e.ObjectInstance = _Service;
        }
        protected void dsPrjects_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
        {
            e.ObjectInstance = _PrjService;
        }
        protected void dsTeams_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
        {
            e.ObjectInstance = _TeamService;
        }
        protected void grvTimeSheets_CustomButtonCallback(object sender, ASPxGridViewCustomButtonCallbackEventArgs e)
        {

        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            Response.Redirect("New.aspx");
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (cbxPrj.Value == null)
                Response.Redirect("Default.aspx");
            else
            {
                Response.Redirect("Default.aspx?prjID=" + cbxPrj.Value, true);
            }
        }
        protected void grvTimeSheets_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
        {
            e.NewValues["Level"] = Session["userrole"];
            e.NewValues["CreateBy"] = Session["UserId"];
            e.NewValues["Week"] = Convert.ToDateTime(e.NewValues["From"]).ToString("MM/dd/yyyy") + " - " + Convert.ToDateTime(e.NewValues["To"]).ToString("MM/dd/yyyy");
        }

        protected void grvTimeSheets_CommandButtonInitialize(object sender, ASPxGridViewCommandButtonEventArgs e)
        {
            if (e.VisibleIndex == -1) return;

            switch (e.ButtonType)
            {
                case ColumnCommandButtonType.Edit:
                    object row = ((ASPxGridView)sender).GetRow(e.VisibleIndex);
                    Timesheet.Domain.TimeSheets timesheet = (Timesheet.Domain.TimeSheets)row;

                    if (timesheet.Role == 2 || timesheet.Role == 3)
                    {
                        e.Visible = true;
                        e.Enabled = EditButtonVisibleCriteria((ASPxGridView)sender, e.VisibleIndex);
                    }
                    else
                    {
                        e.Visible = false;
                    }
                    break;
                case ColumnCommandButtonType.Delete:
                    e.Enabled = DeleteButtonVisibleCriteria((ASPxGridView)sender, e.VisibleIndex);
                    break;
            }

            //if (e.ButtonType == DevExpress.Web.ColumnCommandButtonType.Edit)
            //    e.Visible = false;

            //// disable the selction checkbox
            //if (e.ButtonType == DevExpress.Web.ColumnCommandButtonType.Delete)
            //    e.Visible = false;
        }

        private bool EditButtonVisibleCriteria(ASPxGridView grid, int visibleIndex)
        {
            object row = grid.GetRow(visibleIndex);
            Timesheet.Domain.TimeSheets timesheet = (Timesheet.Domain.TimeSheets)row;

            if (timesheet.Role == 2)
            {
                if (timesheet.Status == "1")
                {
                    return true;
                }
            }
            else if (timesheet.Role == 3)
            {
                if (timesheet.Status == "2" || timesheet.Status == "1")
                {
                    return true;
                }
            }
            return false;

        }

        private bool DeleteButtonVisibleCriteria(ASPxGridView grid, int visibleIndex)
        {
            object row = grid.GetRow(visibleIndex);
            Timesheet.Domain.TimeSheets timesheet = (Timesheet.Domain.TimeSheets)row;

            if (timesheet.Status == "0")
            {
                if (timesheet.Role == 2 || timesheet.Role == 3 || timesheet.Role == 1)
                    return true;
            }
            else if (timesheet.Status == "1")
            {
                if (timesheet.Role == 2)
                {
                    return true;
                }
            }
            else if (timesheet.Status == "2")
            {
                if (timesheet.Role == 3)
                {
                    return true;
                }
            }
            return false;

        }

        protected void grvTimeSheets_CellEditorInitialize(object sender, ASPxGridViewEditorEventArgs e)
        {
            if (e.Column.FieldName == "ProjectID")
                e.Editor.ClientEnabled = false;
        }

        protected void grvTimeSheets_CustomErrorText(object sender, ASPxGridViewCustomErrorTextEventArgs e)
        {
            if (e.Exception is NullReferenceException)
                e.ErrorText = "NullReferenceException Text";
            else if (e.Exception is InvalidOperationException)
                e.ErrorText = "InvalidOperationException Text";
            else if (e.Exception is Exception)
            {
                if (e.Exception.InnerException is CustomException)
                {
                    e.ErrorText = e.Exception.InnerException.Message;
                }
                else
                    e.ErrorText = e.Exception.Message;
            }
        }

    }
}